<?php
class UserAction {
	public function login() {
		if ($_POST ['login']) {
			if (! Base::$oBase->Image_checkValidateCode ( $_POST ['validate'] )) {
				Base::$oBase->Template_assign ( 'error', '验证码错误' );
			} elseif (Base::$oBase->User_login_model ( $_POST ['name'], $_POST ['password'] ) > 0) {
				header ( 'location:index.php' );
				exit ();
			} else {
				Base::$oBase->Template_assign ( 'error', '用户名不存在或密码错误' );
			}
		}
		
		Base::$oBase->Template_assign ( 'title', '登录' );
		Base::$oBase->Template_output ( 'login.html' );
	}
	
	public function logout() {
		Base::$oBase->User_logout_model ();
		header ( 'location:./' );
	}
	
	public function password() {
		if ($_POST ['save']) {
			if (trim ( $_POST ['newpassword'] )) {
				if ($_POST ['newpassword'] == $_POST ['repassword']) {
					$sPassword = Base::$oBase->User_encodePassword_model ( $_POST ['password'] );
					if ($sPassword == $_SESSION ['user'] ['password']) {
						Base::$oBase->User_changePassword_model ( $_SESSION ['user'] ['id'], trim ( $_POST ['newpassword'] ) );
						Base::$oBase->Template_assign ( 'error', '密码修改成功' );
					} else {
						Base::$oBase->Template_assign ( 'error', '密码错误' );
					}
				} else {
					Base::$oBase->Template_assign ( 'error', '新密码和重复密码不同' );
				}
			} else {
				Base::$oBase->Template_assign ( 'error', '密码不能为空' );
			}
		}
		Base::$oBase->Template_output ( 'password.html' );
	}
	
	public function index() {
		global $sMethod;
		$sMethod = 'index';
		$aMethod = array ('m', 'id' );
		if ($_POST ['filter']) {
			$aMethod ['status'] = $_POST ['status'];
			$_GET ['status'] = $_POST ['status'];
		}
		if ($_GET ['status'] == '') {
			$aStatus = array ('active', 'close' );
		} else {
			$aStatus = array ($_GET ['status'] );
		}
		if (! Base::$oBase->User_checkIsSA_model ( $_SESSION ['user'] ['id'] )) {
			Base::$oBase->User_setFilter_model ( 'auth_id', $_SESSION ['user'] ['id'] );
		}
		$aList = Base::$oBase->User_getList_model ( $_GET ['page'], 25 );
		$aPrivilegeList = Base::$oBase->Privilege_getListNeedAuth_model ();
		Base::$oBase->Template_assign ( 'url', './?' . Base::getSearch ( $aMethod ) );
		if (! $_GET ['m']) {
			$_GET ['m'] = 'add';
		}
		if ($_GET ['m'] == 'add') {
			$aInfo ['privilege'] = array ();
			$aInfo ['authorize'] = array ();
			Base::$oBase->Template_assign ( 'info', $aInfo );
		}
		Base::$oBase->Template_assign ( 'list', $aList );
		Base::$oBase->Template_assign ( 'privilegeList', $aPrivilegeList );
		Base::$oBase->Template_assign ( 'mulitpage', Base::$oBase->Tools_getMulitPage_model ( $aList ['items'], $aList ['pages'], $aList ['page'] ) );
		Base::$oBase->Template_assign ( 'sa', Base::$oBase->User_checkIsSA_model ( $_SESSION ['user'] ['id'] ) );
	}
	
	public function add() {
		if ($_POST ['cancel']) {
			Base::location ( './?' . Base::getSearch ( array ('m', 'id' ) ) );
		} elseif ($_POST ['save'] || $_POST ['apply']) {
			if (! Base::$oBase->User_checkIsSA_model ( $_SESSION ['user'] ['id'] )) {
				$_POST ['auth_id'] = $_SESSION ['user'] ['id'];
			}
			$iUserId = Base::$oBase->User_add_model ( $_POST );
			if ($_POST ['save']) {
				Base::location ( './?' . Base::getSearch ( array ('m', 'id' ) ) );
			} else {
				Base::location ( './?' . Base::getSearch ( array ('id' => $iUserId ) ) );
			}
		}
		
		$aInfo ['privilege'] = array ();
		Base::$oBase->Template_assign ( 'info', $aInfo );
		$this->index ();
	}
	
	public function edit() {
		if (! Base::$oBase->User_checkIsSA_model ( $_SESSION ['user'] ['id'] )) {
			$aInfo = Base::$oBase->User_get_model ( $_GET ['id'] );
			if ($aInfo ['auth_id'] != $_SESSION ['user'] ['id']) {
				$this->index ();
				exit ();
			}
		}
		
		if ($_POST ['cancel']) {
			Base::location ( './' . Base::getSearch ( array ('m', 'id' ) ) );
		} elseif ($_POST ['save'] || $_POST ['apply']) {
			$_POST ['id'] = $_GET ['id'];
			$sPassword = $_POST ['password'];
			unset ( $_POST ['password'] );
			Base::$oBase->User_edit_model ( $_POST );
			if (trim ( $sPassword )) {
				Base::$oBase->User_changePassword_model ( $_POST ['id'], trim ( $sPassword ) );
			}
			if ($_POST ['save']) {
				Base::location ( './?' . Base::getSearch ( array ('m', 'id' ) ) );
			} else {
				Base::location ( './?' . Base::getSearch () );
			}
		}
		
		$aInfo = Base::$oBase->User_get_model ( $_GET ['id'] );
		Base::$oBase->Template_assign ( 'info', $aInfo );
		$this->index ();
	}
	
	public function del() {
		if ($_POST ['confirm']) {
			Base::$oBase->User_del_model ( $_GET ['id'] );
			Base::location ( './?' . Base::getSearch ( array ('m', 'id' ) ) );
		} elseif ($_POST ['cancel']) {
			Base::location ( './?' . Base::getSearch ( array ('m', 'id' ) ) );
		}
		$this->index ();
	}
}